package com.msb.servicemap.remote;

import com.msb.commons.constant.AmapConstant;
import com.msb.commons.response.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
@Slf4j
public class MapDirectionClient {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${amap.key}")
    private String amapKey;

    public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){

        //组装请求调用url
        // https://restapi.amap.com/v3/direction/driving
        // ?origin=116.481028,39.989643&destination=116.465302,40.004717
        // &extensions=base&output=json&key=0e18e92d4fd4b3d09eb13666c11b8004
        StringBuilder urlBuilder = new StringBuilder();

        urlBuilder.append(AmapConstant.DIRECTION_URL);
        urlBuilder.append("?");
        urlBuilder.append("origin="+depLongitude+","+depLatitude);
        urlBuilder.append("&");
        urlBuilder.append("destination="+destLongitude+","+destLatitude);
        urlBuilder.append("&");
        urlBuilder.append("extensions=base");
        urlBuilder.append("&");
        urlBuilder.append("output=json");
        urlBuilder.append("&");
        urlBuilder.append("key="+amapKey);

        log.info(urlBuilder.toString());

        //调用高德接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        String entityBody = directionEntity.getBody();
        log.info("高德地图:路径规划,返回信息: "+entityBody);

        //解析接口
        DirectionResponse directionResponse = parseEntityBody(entityBody);
        return directionResponse;

    }

    private DirectionResponse parseEntityBody(String entityBody){
        DirectionResponse directionResponse = null;

        try {
            //最外层转成json对象
            JSONObject result = JSONObject.fromObject(entityBody);
            if (result.has(AmapConstant.STATUS)){
                int status = result.getInt(AmapConstant.STATUS);
                if (status == 1){
                    if (result.has(AmapConstant.ROUTE)){
                        JSONObject routeObject = result.getJSONObject(AmapConstant.ROUTE);
                        JSONArray pathsArray = routeObject.getJSONArray(AmapConstant.PATHS);
                        JSONObject pathObject = pathsArray.getJSONObject(0);
                        int distance = pathObject.getInt(AmapConstant.DISTANCE);
                        int duration = pathObject.getInt(AmapConstant.DURATION);

                        //响应赋值
                        directionResponse = new DirectionResponse();

                        directionResponse.setDistance(distance);
                        directionResponse.setDuration(duration);
                    }
                }
            }
        }catch (Exception e){

        }
        return directionResponse;

    }
}
